<?php
error_reporting("E_ALL");ini_set("display_errors", 1);
$getdata=$_REQUEST;
if(empty($getdata))$getdata=$_POST?$_POST:$_GET;
$type=empty($getdata['type'])?'':$getdata['type'];
include("include/function.php");
//header("content-Type: application/json; charset=utf-8");
$config = include("config.php");
include("include/".$config['DB_TYPE'].".class.php");
$db = new db;
$db->connect($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'], $config['DB_PCONNECT']);
$school = include("data/school.php");
        
$page = empty($getdata['page'])?1:$getdata['page'];
$id = empty($getdata['id'])?0:$getdata['id'];
$length=50;
$where="personType=1";	
if($id){
    $where.=" and id>".$id;
}
function getunitid($name,$school){
    foreach($school as $k=>$v){
        if($v['name']==$name)return $k;
    }
    return false;
}
$orderids=[];
$insert=0;
$sql="select * from order_info_hs where ".$where.' order by id limit '.$length;
$list = $db->get_all($sql);if(empty($list))exit('over');
foreach($list as $v){$id=$v['id'];
    if(in_array($id,$orderids))continue;
    if(empty($v['unitid']))$v['unitid']=getunitid($v['unitName'],$school);
    $school_id = !empty($school[$v['unitid']]['school_id'])?$school[$v['unitid']]['school_id']:0;
    if(empty($school_id)){
        print_r($v);exit;
    }
    $check = $db->get_one("select `student_id` from hema_student where school_id=".$school_id." and `name`='".$v['name']."'");
    if(empty($check['student_id'])){
        $data=[];
        $data['uuid']=$v['userId'];
        $data['name']=$v['name'];
        $data['phone']=$v['phone'];
        $data['school_id']=$data['shop_id']=$data['applet_id']=$data['class_id']=0;
        if(!empty($school[$v['unitid']])){
            $data['school_id']=$school[$v['unitid']]['school_id'];
            $data['shop_id']=$school[$v['unitid']]['shop_id'];
            $data['applet_id']=$school[$v['unitid']]['applet_id'];
            
        }
        $data['type']=$v['personType'];
        $v['createtime']=str_replace(' 000','',$v['createtime']);
        $data['create_time']=$data['update_time']=strtotime($v['createtime']);
        $ssql = "insert into hema_student(`".implode('`,`',array_keys($data))."`) values('".implode("','",$data)."');";
        $res = $db->query($ssql);
        if(!$res){
            echo $id;
            exit;
        }
        $insert++;
    }
}
$log = $id.'导入成功,新增：'.$insert.'条';
echo $log.'<script>location.href="?page='.$page.'&id='.$id.'";</script>';

//echo count($sqlarr)."\n";
//echo implode(";\n",$sqlarr);
?>